#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;

vector<ll> p[1000100];
bool vis[1000100];
ll n,m,k;

void dfs(ll id){
    vis[id]=true;
    ll len=p[id].size();
    for(ll j=0;j<len;j++){
        ll y=p[id][j];
        if(vis[y])continue;
        dfs(y);
    }
    return ;
}

int main()
{
    IOS;
    cin>>n>>m;
    cin>>k;
    ll x,y;
    for(ll i=1;i<=k;i++){
        cin>>x>>y;
        p[x].push_back(y);
        p[y].push_back(x);
    }
    ll ans=0,sum=n*m;
    for(ll i=1;i<=sum;i++){
        if(vis[i])continue;
        ans++;
        dfs(i);
    }
    cout<<ans<<endl;
    getchar();
    getchar();
    return 0;
}
